import NProgress from "nprogress";
import "nprogress/nprogress.css";

import { createRouter, createWebHashHistory } from 'vue-router'
import routes from './routers'
NProgress.configure({
  ease: "ease",
  speed: 100,
});

import { getToken } from '@/utils/util'
import config from '@/config'
import store from '@/store'


const { writeNames } = config
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});


router.beforeEach((to, from, next) => {
  console.log('store',store);
  
  document.title = config.title
  NProgress.start();
  const token = getToken()
  // 如果登录了 然后想去登录页 就让他去首页
  if (token) {
    if (to.path === "/login") {
      next("/home");
    }else{
      return next();
    }
  } else {
    // 如果没登录  去是不需要登录的页面（白名单）就放行，如果去其他的就自动跳转到登录
    if (writeNames.includes(to.path)) {
      next();
    } else {
      next("/login");
    }
  }
})

router.afterEach(to => {
  NProgress.done();
  window.scrollTo(0, 0)
})

export default router;
